package de.se.ludoapp.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import de.se.ludoapp.LudoApplication;
import de.se.ludoapp.R;
import de.se.ludoapp.activity.task.LoginLogoutRegisterTask;
import de.se.ludoapp.gaming.LudoSystemStub;

/**
 * Login Activity (Startpage)
 * 
 * @author L.Rother
 * 
 */
public class LoginRememberActivity extends Activity {

	private LudoApplication ludoApp;

	private String username, password;
	private EditText editTextUsername, editTextPassword;
	private Button ok;
	private CheckBox box;
	private SharedPreferences loginPreferences;
	private SharedPreferences.Editor loginPrefsEditor;
	private Boolean saveLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// // Initialise Stub LudoServer
		ludoApp = (LudoApplication) this.getApplication();
		ludoApp.setLudoStub(new LudoSystemStub());

		// View
		setContentView(R.layout.login_reg);
		ok = (Button) findViewById(R.id.buttonRegLogin);

		editTextUsername = (EditText) findViewById(R.id.usernameLoginInput);
		editTextPassword = (EditText) findViewById(R.id.passwordLoginInput);
		box = (CheckBox) findViewById(R.id.rememberMe);

		loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
		loginPrefsEditor = loginPreferences.edit();

		saveLogin = loginPreferences.getBoolean("saveLogin", false);
		// if userPreferences were saved before, the username and password are
		// filled out automatically
		if (saveLogin == true) {
			editTextUsername
					.setText(loginPreferences.getString("username", ""));
			editTextPassword
					.setText(loginPreferences.getString("password", ""));
			box.setChecked(true);
		}

		// This is the link to change to the Registration Screen
		TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
		registerScreen.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// Switching to Register screen
				Intent i = new Intent(getApplicationContext(),
						RegistrationActivity.class);
				startActivity(i);
			}
		});

		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				username = editTextUsername.getText().toString();
				password = editTextPassword.getText().toString();

				new LoginLogoutRegisterTask(ludoApp,
						LoginRememberActivity.this, username, password)
						.execute();

				ok.setEnabled(false);
				TextView loginResult = (TextView) findViewById(R.id.usernameLogin);
				loginResult.setText("Bitte warten. Sie werden eingeloggt.");

				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(editTextUsername.getWindowToken(),
						0);

				// if the user wants to be remembered, his login data is saved
				// into the shared preferences
				if (box.isChecked()) {
					loginPrefsEditor.putBoolean("saveLogin", true);
					loginPrefsEditor.putString("username", username);
					loginPrefsEditor.putString("password", password);
					loginPrefsEditor.commit();
				} else {
					loginPrefsEditor.clear();
					loginPrefsEditor.commit();
				}

			}

		});

		// Menue Button
		Button buttonMenue = (Button) findViewById(R.id.buttonMainMenueReg);
		if (ludoApp.getlogedinUser() != null) {
			buttonMenue.setVisibility(View.VISIBLE);
			ok.setText(R.string.buttonLogout);
		} else {
			buttonMenue.setVisibility(View.GONE);
			ok.setText(R.string.buttonLogin);
		}
		buttonMenue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intentSelectMenu = new Intent(
						LoginRememberActivity.this, Select_menuActivity.class);
				startActivity(intentSelectMenu);
			}

		});

	}

	/**
	 * Start GameActivity
	 */
	protected void startActivityNewGame() {
		Intent intent = new Intent(this, GameActivity.class);
		startActivity(intent);
	}

}
